package org.vordel.incubator.examples.management_api;

import java.util.Collection;

import com.vordel.es.ESPK;
import com.vordel.es.Entity;
import com.vordel.es.EntityStore;
import com.vordel.es.EntityType;
import com.vordel.es.Field;
import com.vordel.es.FieldType;
import com.vordel.es.Value;

public abstract class EntityWrapperManager {

	protected ESPK containerESPK;
	protected Session session;

	EntityWrapperManager(Session session) {
		super();
		this.session = session;
	}
	
	protected abstract String getContainerEntityTypeName();
	protected abstract String getContainerEntityName();
	protected abstract String getEntityTypeName(); 
	
   protected ESPK getContainerESPK() throws Exception {
		
		if (containerESPK==null) {
			EntityStore es = session.getEntityStore();
			
			ESPK rootPK = es.getRootPK();
			
			if (this.getContainerEntityName().equals("System Components")) {
				this.containerESPK = es.getRootPK();
				return rootPK;
			}
			
			EntityType containerType = es.getTypeForName(this.getContainerEntityTypeName());
			System.out.println(containerType.getAllFieldNames());
        
			FieldType nameFieldType = containerType.getFieldType("name");
			Field nameField = new Field(nameFieldType,"name");
			nameField.addValue(new Value(this.getContainerEntityName()));
        
			Collection<ESPK> children = es.findChildren(rootPK, new Field[] {nameField}, containerType);
        
			this.containerESPK = children.iterator().next();
			
		}
        
		return containerESPK;
		
		
	}
  
   public Entity getEntityByName(String name, EntityWrapper entityWrapper, String entityType) throws Exception {
	   
	   return this.getEntityByName(name, entityWrapper.getEntity().getPK(),entityType);
   }
   public Entity getEntityByName(String name, EntityWrapper entityWrapper) throws Exception {
	   
	   return this.getEntityByName(name, entityWrapper.getEntity().getPK(),this.getEntityTypeName());
   }
   public Entity getEntityByName(String name) throws Exception {
	   
	   return this.getEntityByName(name, this.getContainerESPK(),this.getEntityTypeName());
	   
   }
   
   protected Entity getEntityByName(String  name, ESPK parentKey, String entityTypeName) throws Exception {
	   
	    Entity entity = null;
		EntityStore es = session.getEntityStore();
		
		EntityType entityType = es.getTypeForName(entityTypeName);
		//Entity entity = new Entity(entityType);
		//System.out.println(entityType.getAllFieldNames());

		FieldType nameFieldType = entityType.getFieldType("name");
		Field nameField = new Field(nameFieldType,"name");
		nameField.addValue(new Value(name));
    
		Collection<ESPK> children = es.findChildren(parentKey, new Field[] {nameField}, entityType);
    
		if (children.size()==0) {
			return null;
		}
		
		ESPK entityESPK = children.iterator().next();

		if (entityESPK!=null) {
			
			return es.getEntity(entityESPK);
			
		} else {
			return null;
		}
		
   }
   
   public Entity createEntity(String name) throws Exception {
	   
		return this.createEntity(name, this.getContainerESPK(),this.getEntityTypeName());   
   }
   
   public Entity createEntity(String name, EntityWrapper parent) throws Exception {
	   
	   return this.createEntity(name, parent.getEntity().getPK(),this.getEntityTypeName());
   }
   
   public Entity createEntity(String name, EntityWrapper parent, String entityType) throws Exception {
	   
	   return this.createEntity(name, parent.getEntity().getPK(), entityType);
   }
   
   private Entity createEntity(String name, ESPK pk, String entityTypeName) throws Exception {
	   
	   EntityStore es = session.getEntityStore();
		
		EntityType entityType = es.getTypeForName(entityTypeName);
		Entity entity = new Entity(entityType);
		//System.out.println(entityType.getAllFieldNames());
		
		FieldType nameFieldType = entityType.getFieldType("name");
		Field nameField = new Field(nameFieldType,"name");
		nameField.addValue(new Value(name));
   
		entity.setField(nameField);
		ESPK entityPK = es.addEntity(pk, entity);
		
		entity = es.getEntity(entityPK);
		
		return entity;
	   
   }

   Session getSession() {
	 return session;
   }


}
